package com.idealighter.game.backpack.manager;

import com.google.inject.Inject;
import com.google.inject.Singleton;

import com.idealighter.game.backpack.dto.ItemInfo;
import com.idealighter.game.backpack.message.ResItemInfoMsg;
import com.idealighter.game.backpack.message.ResItemInfosMsg;
import com.idealighter.game.backpack.message.ResUsedItemMsg;
import com.idealighter.game.player.manager.PlayerMsgWriter;
import com.idealighter.game.player.struct.Player;

import java.util.Map.Entry;

/**
 * 物品消息管理 .
 * 
 * @date 2015年9月2日 下午8:05:21
 *
 */
@Singleton
public class BackpackMsgMgr {

  @Inject
  private PlayerMsgWriter msgWriter;

  /**
   * 发送玩家身上所有的道具 .
   * 
   * @param player 玩家.
   */
  public void noticeItemInfosMsg(Player player) {
    ResItemInfosMsg msg = new ResItemInfosMsg();

    for (Entry<Integer, Integer> itemEtr : player.items().entrySet()) {
      ItemInfo itemInfo = new ItemInfo();
      itemInfo.setId(itemEtr.getKey());
      itemInfo.setNum(itemEtr.getValue());
      msg.getItems().add(itemInfo);
    }

    msgWriter.writeMsg(player, msg);
  }

  /**
   * 发送玩家身上指定的道具 .
   * 
   * @param player 玩家.
   * @param itemId .
   */
  public void sendItemInfoMsg(Player player, int itemId) {
    ResItemInfoMsg msg = new ResItemInfoMsg();
    ItemInfo item = new ItemInfo();
    item.setId(itemId);
    item.setNum(player.items().get(itemId));
    msg.setItem(item);

    msgWriter.writeMsg(player, msg);
  }

  /**
   * 发送被使用道具消息 .
   * 
   * @param player 玩家.
   * @param usePlayer .
   * @param itemId .
   * @param num .
   */
  public void sendUsedItemMsg(Player player, Player usePlayer, int itemId, int num) {
    ResUsedItemMsg msg = new ResUsedItemMsg();
    msg.setItemId(itemId);
    msg.setNum(num);
    msg.setPlayerId(usePlayer.getId());

    msgWriter.writeMsg(player, msg);
  }

}
